package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.MessageConstant;
import com.itheima.entity.AddressBook;
import com.itheima.mapper.AddressMapper;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户地址模块-业务实现
 */

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 地址新增
     *
     * @param addressBook
     * @return
     */
    @Override
    public int saveAddress(AddressBook addressBook) {
        //校验参数
        int row = 0;
        //如果存入数据等于null则返回0,返回0就是数据存入失败
        if (addressBook == null) {
            return row;

        }
        //补全数据user_id(用户id)
        //从当前线层获取用户ID
        //登录还没写,将从线程获取的id,写死
        Long userId = BaseContext.getUserId();
        if (userId == null) {
            throw new RuntimeException(MessageConstant.NOT_LOGIN);
        }
        addressBook.setUserId(userId);
//        addressBook.setCreateTime(LocalDateTime.now());
//        addressBook.setUpdateTime(LocalDateTime.now());
//        addressBook.setCreateUser(1L);
//        addressBook.setUpdateUser(1L);
        //判断当前地址数量
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);
        List<AddressBook> addressBookList = addressMapper.selectList(wrapper);
        if(addressBookList.size()> 10){
            return row;

        }
        row = addressMapper.insert(addressBook);
        return row;
    }

    /**
     * 查询当前登录用户地址列表,默认地址排在最上面
     */
    @Override
    public List<AddressBook> list() {
        //获取当前登录用户ID
        Long userId = BaseContext.getUserId();

        //封装排序请求条件
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, userId);
        wrapper.orderByDesc(AddressBook::getIsDefault, AddressBook::getCreateTime);
        //执行查询
        List<AddressBook> addressBooksList = addressMapper.selectList(wrapper);

        return addressBooksList;
    }

    /**
     * 修改默认地址
     */
    @Override
    public int defaultAddr(Long id) {
        int row = 0;
        if (id == null) {
            return row;
        }
        //获取当前登录Id
        Long userId = BaseContext.getUserId();
        if (userId == null) {
            return row;
        }

        /**
         * 设置用户不是默认地址
         */
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, userId);
        updateWrapper.set(AddressBook::getIsDefault, 0);
        addressMapper.update(new AddressBook(), updateWrapper);
        //设置当前地址为默认地址

        //查询用户id
        updateWrapper.eq(AddressBook::getUserId, userId);
        //查询id
        updateWrapper.eq(AddressBook::getId, id);
        //设置默认
        updateWrapper.set(AddressBook::getIsDefault, 1);
        row = addressMapper.update(new AddressBook(), updateWrapper);
        return row;
    }

    /**
     * 获取默认地址
     */
    @Override
    public AddressBook getDefaultAddr() {
        //获取去当前登录用户的ID
        Long userId = BaseContext.getUserId();
        //构建查询条件
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId, userId);
        wrapper.eq(AddressBook::getIsDefault, 1);
        //执行查询
        AddressBook addressBook = addressMapper.selectOne(wrapper);

        return addressBook;
    }


    /*
    地址删除
     */
    @Override
    public int dedeleteAddress(Long ids) {
        int row = 0;
        if (ids == null) {
            return row;
        }
        row = addressMapper.deleteById(ids);
        return row;
    }

    /**
     * 根据Id获取当前地址
     * @param :addressBook
     * @return
     */
    @Override
    public AddressBook getAdderss(Long id){
        Long userId = BaseContext.getUserId();
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,userId);
        AddressBook addressBook = addressMapper.selectById(id);
        return addressBook;

    }
    /**
     * 更新地址
     */
    @Override
    public int update(AddressBook addressBook) {
        int row = 0;
        if (addressBook == null) {
            return row;
        }
        row = addressMapper.updateById(addressBook);
return row;
    }
}
/**
 * @BelongsProject: reggie_project
 * @BelongsPackage: com.itheima.service.impl
 * @Author: luanyinghao
 * @CreateTime: 2022-06-05  16:42
 * @Description: TODO
 * @Version: 1.0
 **/